/**==============================================================================
            Copyright (c) 2013 Moritz Hader, Marcel Schrder
            All Rights Reserved.

	@file  MoeMessageObject.java

	@brief Collection of Information-System objects. Distinguishes the different
	states of the objects by separate lists.

  This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
==============================================================================*/
package de.ibm.dhbw.backing.dataHolding;

import java.util.ArrayList;

public class MoeMessageObject {
	
	//----------- Properties and Constructor -----------//

	private ArrayList<ElementObject> newElements;
	private ArrayList<ElementObject> changedElements;
	private ArrayList<Integer> discardingElementIds;
	
	private ArrayList<ElementObject> dynamicElements;
	
	public MoeMessageObject(ArrayList<ElementObject> oldElementList, ArrayList<ElementObject> newElementList) {
		this.newElements = new ArrayList<ElementObject>();
		this.changedElements = new ArrayList<ElementObject>();
		this.discardingElementIds = new ArrayList<Integer>();
		
		this.dynamicElements = new ArrayList<ElementObject>();
		
		for(ElementObject n : newElementList) {
			if(oldElementList.contains(n)) {
				oldElementList.remove(n);
				continue;
			} else {
				boolean breaked = false;
				for(ElementObject m : oldElementList) {
					if(n.getId() == m.getId()) {
						this.changedElements.add(n);
						if(n instanceof DynamicElementObject) {
							if(((DynamicElementObject) n).getMovement() < 0.0001) {
								n.setDirectionInDegrees(m.getDirectionInDegrees());
							} else {
								this.dynamicElements.add(n);
							}
						}
						breaked = true;
						oldElementList.remove(m);
						break;
					}
				}
				if(!breaked) {
					this.newElements.add(n);
					if(n instanceof DynamicElementObject && ((DynamicElementObject) n).getMovement() >= 0.0001) {
						this.dynamicElements.add(n);
					}
				}
			}
		}
		for(ElementObject m : oldElementList) {
			this.discardingElementIds.add(m.getId());
		}
	}
	
	
	//----------- Customised Getter and Setter -----------//

	public ArrayList<ElementObject> getInterpolatedElements(float timePassedInSec) {
		for(ElementObject n : this.dynamicElements) {
			((DynamicElementObject) n).interpolateNewPosition(timePassedInSec);
		}
		return this.dynamicElements;
	}
	
	
	//----------- Simple Getter and Setter -----------//
	
	public ArrayList<ElementObject> getNewElements() {
		return newElements;
	}
	public ArrayList<ElementObject> getChangedElements() {
		return changedElements;
	}
	public ArrayList<Integer> getDiscardingElementIds() {
		return discardingElementIds;
	}
	
}
